import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:my_blog_flutter/app/modules/login/models/login_entity.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../config/app_environment.dart';
import '../modules/res/string_res.dart';
import 'SPUtils.dart';

enum ChatFromType {
  chatFromTypeDefault,
  chatFromTypePush,
}

class UserUtils {
  static Future<void> saveLoginInfo(LoginData entity) async {
    final jsonString = jsonEncode(entity.toJson());
    await SPUtils.saveData(SPKeys.loginInfo, jsonString);
  }

  //
  static Future<LoginData?> getLoginInfo() async {
    final jsonString = await SPUtils.getDataString(SPKeys.loginInfo);
    if (jsonString.isEmpty) {
      return null;
    }
    final jsonMap = jsonDecode(jsonString);
    final entity = LoginData.fromJson(jsonMap);
    return entity;
  }

  // uId
  static Future<String?> getToken() async {
    final myLoginUserInfo = await getLoginInfo();
    return myLoginUserInfo?.token;
  }

  /// 退出登录，清除用户信息
  static Future<void> logout() async {
    await SPUtils.remove(SPKeys.loginInfo);
  }
  //
  // // 学工号 是userId
  // static Future<String?> userId() async {
  //   final myLoginUserInfo = await getLoginInfo();
  //   return myLoginUserInfo?.d?.xgh;
  // }
  //
  // // 登录凭证
  // static Future<String?> loginTicket() async {
  //   final myLoginUserInfo = await getLoginInfo();
  //   return myLoginUserInfo?.d?.loginTicket;
  // }

  // static Future<bool> isLogin() async {
  //   final myUserId = await userId() ?? "";
  //   if (myUserId.isNotEmpty) {
  //     return true;
  //   } else {
  //     return false;
  //   }
  // }

  // static Future<bool> isLoginExpired() async {
  //   final result = await isLogin();
  //   if (result == false) {
  //     return true;
  //   }
  //   final isLoginExpired = await SPUtils.getDataBool(SPKeys.loginExpired);
  //   return isLoginExpired;
  // }

  // 获取用户代理信息
  static Future<String> getUserAgent() async {
    // 设置Header
    String userAgent = '';
    // 拼接前缀
    userAgent = '$userAgent${AppEnvironment.instance.userAgentPre}';

    // 获取包信息
    final info = await PackageInfo.fromPlatform();
    // 版本号
    final version = info.version;
    // 拼接版本号
    userAgent = '$userAgent$version';

    // // 硬件信息
    // DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    // if (defaultTargetPlatform == TargetPlatform.android) {
    //   AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    //   userAgent =
    //       '$userAgent ${androidInfo.model} Android ${androidInfo.version.release}';
    // } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    //   IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    //   userAgent =
    //       '$userAgent ${iosInfo.utsname.machine} iOS ${iosInfo.systemVersion}';
    // } else if (defaultTargetPlatform == TargetPlatform.ohos) {
    //   final ohosDeviceInfo = await deviceInfo.ohosDeviceInfo;
    //   userAgent =
    //       '$userAgent ${ohosDeviceInfo.marketName} HarmonyOS ${ohosDeviceInfo.osFullName} Mobile';
    // }
    return userAgent;
  }
}
